package cn.monkey.family.data.crud.repository;

import cn.monkey.family.data.QueryRequest;
import com.querydsl.core.types.Predicate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.ListQuerydslPredicateExecutor;

import java.util.List;
import java.util.Optional;

public interface QueryDslQueryRequestRepository<T, ID, Q extends QueryRequest> extends QueryRequestRepository<T, ID, Q>,
        ListQuerydslPredicateExecutor<T> {

    Predicate queryDslPredicate(Q queryRequest);


    @Override
    default Optional<T> findOneByQueryRequest(Q queryRequest) {
        return this.findOne(this.queryDslPredicate(queryRequest));
    }

    @Override
    default Page<T> findPageByQueryRequest(Q queryRequest, Pageable pageable) {
        return this.findAll(this.queryDslPredicate(queryRequest), pageable);
    }

    @Override
    default List<T> findAllByQueryRequest(Q queryRequest, Sort sort) {
        return this.findAll(this.queryDslPredicate(queryRequest), sort);
    }
}
